﻿/*******************************************************************  
 * Copyright (C) vancl.com
 * All rights reserved. 
 * 
 * Created By: VANCLOA\hanxianlong
 * Create Date:2011/8/16 15:49:45
 * Last Modified By:VANCLOA\hanxianlong
 * Last Modified Date:2011/8/16 15:49:45
 * Description: 代码生成工具
 * ********************************************************************/
#region Using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;
using SimpleCodeGenerator.Schema;
using SimpleCodeGenerator.Utility;
#endregion

namespace SimpleCodeGenerator.Generators
{
	public abstract class GeneratorBase
	{
		/// <summary>
		/// 输出的文件夹
		/// </summary>
		public string OutputPath { get; set; }

		/// <summary>
		/// 顶级命名空间
		/// </summary>
		public string Namespace { get; set; }

		/// <summary>
		/// 生成的方法，需要具体实现
		/// </summary>
		/// <param name="element">element</param>
		public abstract void Generate(TableSchema table);

		/// <summary>
		/// 生成版本信息（类的头部信息）
		/// </summary>
		/// <param name="output">将生成的信息输出到的StringBuilder</param>
		protected void BuildCopyRight(MyStringBuilder output)
		{
			//append copyright
			output.AppendLine("/" + new string('*', 80));
			output.IncreaseIndent();
			output.AppendLine("AUTO GENERATED BY SIMPLE CODE GENERATOR, PLEASE DON'T MODIFY THIS CODE MANUALLY");
			output.AppendLine("GENERATED ON:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
			output.AppendLine("ALL RIGHTS RESERVED. VANCL.COM");
			output.DecreaseIndent();
			output.AppendLine(new string('*', 80) + "/");

			output.AppendLine();
		}

		protected void BuildCommonUsingBlock(MyStringBuilder output)
		{
			output.AppendLine("using System;");
			output.AppendLine("using System.Collections.Generic;");
			output.AppendLine("using System.Linq;");
			output.AppendLine("using System.Text;");
		}

		protected void BuildMoreUsingBlock(MyStringBuilder output, string usingNamespace)
		{
			output.AppendFormat("using {0};", usingNamespace).AppendLine();
		}

		protected void BuildRemark(MyStringBuilder output, string remark)
		{
			output.AppendLine("/// <summary>");
			output.AppendFormat("/// {0}", remark)
				.AppendLine();
			output.AppendLine("/// </summary>");
		}

		protected void BuildParamRemark(MyStringBuilder output, string paramName, string paramRemark)
		{
			output.AppendFormat( "/// <param name=\"{0}\">{1}</param>",paramName,paramRemark)
				.AppendLine();
		}

		protected void BuildReturnRemark(MyStringBuilder output, string remark)
		{
			output.AppendFormat("/// <returns>{0}</returns>", remark).AppendLine();
		}

		/// <summary>
		/// 保存至文件
		/// </summary>
		/// <param name="fileName">文件名(无需带扩展名)</param>
		/// <param name="content">文件内容</param>
		protected void Save(string fileName, string content)
		{
			if (!Directory.Exists(OutputPath))
			{
				Directory.CreateDirectory(OutputPath);
			}
			File.WriteAllText(OutputPath + "/" + fileName + ".cs", content);
		}
	}
}
